﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Acoris.Markdown
{
    /// <summary>
    /// 表示解析上下文
    /// </summary>
    internal class AmlParseContext
    {
        Stack<AMLParseNode>     m_contextStack;
        Stack<AmlDocumentState> m_stateStack;
        AmlParseFrameStack      m_parseFrameStack;
        AMLParseNode            m_root;

        public AmlParseContext()
        {
            m_contextStack = new Stack<AMLParseNode>();
            m_parseFrameStack = new AmlParseFrameStack();
            m_stateStack = new Stack<AmlDocumentState>();
        }

        #region Scope

        /// <summary>
        /// 结束一个作用域
        /// </summary>
        internal void EndScope()
        {
            var node =  m_contextStack.Pop();
            if(m_contextStack.Count > 0) {
                var parent = m_contextStack.Peek();
                parent.Children.Add(node);
            }
        }

        /// <summary>
        /// 开始一个作用域
        /// </summary>
        /// <param name="node"></param>
        internal void BeginScope(AMLParseNode node)
        {
            if(m_root == null) {
                m_root = node;
            }
            m_contextStack.Push(node);
        }

        /// <summary>
        /// 获取当前作用域
        /// </summary>
        internal AMLParseNode Scope {
            get { return m_contextStack.Count == 0 ? null : m_contextStack.Peek(); }
        }

        #endregion

        #region State

        internal AmlDocumentState State { get; set; }

        internal void PushState(AmlDocumentState state)
        {
            m_stateStack.Push(state);
        }

        internal void PopState()
        {
            State = m_stateStack.Pop();
        }
        #endregion

        internal AMLParseNode Root { get { return m_root; } }

        internal void Reset() { m_root = null; }

        internal AmlParseFrameStack ParseFrameStack { get { return m_parseFrameStack; } }
    }
}
